package com.hoodbrains.insideloc.guidedshopping;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.hoodbrains.insideloc.R;
import com.hoodbrains.insideloc.maps.CreateMapActivity;
import com.hoodbrains.insideloc.maps.Liste;
import com.hoodbrains.insideloc.maps.Magasin;
import com.hoodbrains.insideloc.maps.UI.DrawingView;
import com.hoodbrains.insideloc.shopping.ShoppingItem;
import com.hoodbrains.insideloc.shopping.ShoppingList;
import com.hoodbrains.insideloc.shopping.ShoppingListActivity;
import com.hoodbrains.insideloc.utils.Catalog;
import com.hoodbrains.insideloc.utils.ItemHolder;

public class ShowMapActivity extends Activity implements OnItemClickListener,
		OnClickListener {

	private ShoppingList model;
	private ProductListAdapter sAdapter;
	private Bitmap btClient,btProduct;
	private DrawingView dv;
	private ListView list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.show_map);
		this.btClient  = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.client);
		this.btProduct = BitmapFactory.decodeResource(getBaseContext().getResources(),R.drawable.product);
		dv = (DrawingView) findViewById(R.id.map_sm);
		//dv.setBackgroundColor(0xFFFFFFFF);

		String osmId = Catalog.getInstance().getCurrentSuperMarketID();
		Magasin magasin; 
			
		if (osmId != null) {
			magasin = loadMagasinFromSD(osmId);
		} else {
			magasin = loadMagasin("supermarket.xml");
		}
		dv.setSupermarket(magasin);
		model = ShoppingList.getInstance();

		Liste liste = new Liste(magasin);
		liste.client = this.btClient;
		liste.product = this.btProduct;
		dv.setListe(liste);

		sAdapter = new ProductListAdapter(this, this, model);
		list = (ListView) findViewById(R.id.products_list_sm);
		list.setAdapter(sAdapter);

		list.setOnItemClickListener(this);
		
		
//			ArrayList<String> res = Catalog.getInstance().getRayons();
//			for(int index=0; index<res.size(); index++){
//				Catalog.getInstance().getProducts(res.get(index));
//			}
				
			
//			Intent i = new Intent(this, ShoppingListActivity.class);
//			startActivityForResult(i, SPLASH_AD_ACTIVITY_CODE);
//			startActivity(i);
//			Intent intent = new Intent(this, RayonChoiceActivity.class);
//			startActivity(intent);
	    }
	
		private Magasin loadMagasinFromSD(String id) {
			Document doc = Catalog.getInstance().getMap(id);
			Magasin magasin = Magasin.deserialize(doc.getDocumentElement());
			return magasin;
		}
	
	private Magasin loadMagasin(String file) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;

		Magasin magasin = null;
		try {
			db = dbf.newDocumentBuilder();
			Document doc = db.parse(getAssets().open(file));
			Element magasinElem = doc.getDocumentElement();
			magasin = Magasin.deserialize(magasinElem);

		} catch (ParserConfigurationException e) {
			Log.e("parse", "1");
			e.printStackTrace();
		} catch (SAXException e) {
			Log.e("parse", "2");
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			Log.e("parse", "3");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return magasin;
	}

	public void onClick(View v) {

		ItemHolder<ShoppingItem> iH = null;
		switch (v.getId()) {
		case R.id.checkbox_sli:
			model.toggleGrabbed(ItemHolder.<ShoppingItem> getItem(v));
			dv.getListe().update();
			dv.invalidate();
			break;
		}
	}

	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		// if (position == 0) {
		// Intent intent = new Intent(this, RayonChoiceActivity.class);
		// startActivityForResult(intent, ADD_PRODUCT_ACTIVITY_CODE);
		//			
		// } else {
		// model.toggleGrabbed(ItemHolder.<ShoppingItem>getItem(v));
		// }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.showmap_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.view_shopping_list_menu: {

			Intent i = new Intent(this, ShoppingListActivity.class);
			startActivity(i);
			return true;
		}
		
		case R.id.edit_map_menu: {
			
			Intent i = new Intent(this, CreateMapActivity.class);
			startActivity(i);
			finish();
			return true;
		}

		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
